package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.DialogUtil
import java.util.concurrent.TimeUnit


class FilterOrderDialog01(var mContext: BaseActivity) : Dialog(mContext, R.style.ChooseDialog) {

    private var mFilterOrderDialog01Listener: FilterOrderDialog01Listener? = null

    private var mStartTimeTv: TextView? = null
    private var mEndTimeTv: TextView? = null

    private var mType1Tv: TextView? = null
    private var mType2Tv: TextView? = null
    private var mType3Tv: TextView? = null
    private var mType4Tv: TextView? = null
    private var mDealTypeTv: TextView? = null
    private var mDealStatusTv: TextView? = null
    private var mPayType: TextView? = null
    private var mCancelTv: TextView? = null
    private var mConfirmTv: TextView? = null

    private var mCloseIv: ImageView? = null

    var mChooseDateDialog: ChooseDateDialog? = null
    private var mDateBegin: String? = null
    private var mDateEnd: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_filter_order)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mEndTimeTv = findViewById(R.id.dialog_end_time_tv)
        mStartTimeTv = findViewById(R.id.dialog_start_time_tv)

        mType1Tv = findViewById(R.id.mType1Tv)
        mType2Tv = findViewById(R.id.mType2Tv)
        mType3Tv = findViewById(R.id.mType3Tv)
        mType4Tv = findViewById(R.id.mType4Tv)

        mDealTypeTv = findViewById(R.id.dialog_deal_type_tv)
        mDealStatusTv = findViewById(R.id.dialog_deal_status_tv)
        mPayType = findViewById(R.id.dialog_pay_type_tv)

        mCancelTv = findViewById(R.id.dialog_cancel_tv)
        mConfirmTv = findViewById(R.id.dialog_confirm_tv)

        mCloseIv = findViewById(R.id.dialog_close_iv)

        mCloseIv?.setOnClickListener {
            dismiss()
        }


        initListener()
        initChooseDate()
    }

    @SuppressLint("CheckResult")
    private fun initChooseDate() {
        //筛选日期
        mDateBegin = CommUtils.toYMD(System.currentTimeMillis() / 1000 /*- 8 * 24 * 3600*/)
        mDateEnd = CommUtils.toYMD(System.currentTimeMillis() / 1000)
        mStartTimeTv?.text = mDateBegin
        mEndTimeTv?.text = mDateEnd
        mStartTimeTv?.setOnClickListener { showChooseDate() }
        mEndTimeTv?.setOnClickListener { showChooseDate() }
    }

    private fun showChooseDate() {
        if (mChooseDateDialog == null) mChooseDateDialog = ChooseDateDialog(mContext)
        mChooseDateDialog?.setOnChooseDateDialogListener(object : ChooseDateDialog.OnChooseDateDialogListener {
            override fun hadSelectedCalendar(mStartDateStr: String, mEndDateStr: String) {
                mStartTimeTv?.text = mStartDateStr
                mEndTimeTv?.text = mEndDateStr
                mDateBegin = mStartDateStr
                mDateEnd = mEndDateStr
            }
        })
        mChooseDateDialog?.show()
    }


    private fun initListener() {
        mCancelTv?.setOnClickListener { dismiss() }

        mConfirmTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    mFilterOrderDialog01Listener?.onFilterClick()
                }
        }
    }


    fun setFilterOrderDialog01Listener(l: FilterOrderDialog01Listener) {
        mFilterOrderDialog01Listener = l
    }


    interface FilterOrderDialog01Listener {
        fun onFilterClick()
    }
    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
